<template>
  <PageWrapper>
    <template #extra>
      <el-button type="primary" @click="handleCreate">新增</el-button>
    </template>
    <template #content>
      <el-table :data="listData"  style="width: 100%; margin-bottom: 20px" border
                default-expand-all row-key="id">
        <el-table-column prop="id" label="id" width="180" />
        <el-table-column prop="name" label="名称" width="180" />
        <el-table-column prop="slug" label="标识" width="180" />
        <el-table-column prop="createdAt" label="新增时间" />
        <el-table-column fixed="right" label="Operations" width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </PageWrapper>

  <el-dialog v-model="dialogVisible" title="新增/编辑" width="500" draggable>
    <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        label-width="auto"
        class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="标识" prop="slug">
        <el-input v-model="ruleForm.slug" autocomplete="off" />
      </el-form-item>
      <el-form-item label="父级" prop="pid">
        <el-cascader :props="selectProps" :options="selectPermissionList" @change="handleChange" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handelSubmit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script setup lang="js">
import PageWrapper from "@/components/pageWrapper/index.vue";
import {reactive, ref} from "vue";
import {createPermission, detail, getList, updatePermission, selectPermission} from "@/api/permission.js";
import {ElMessage} from "element-plus";
const formInline = reactive({})
const dialogVisible = ref(false)
let ruleForm = reactive({
  id:'',
  name: '',
  slug: ''
})
const selectProps = reactive({
  value: 'id',
  label: 'name',
  children: 'children',
  checkStrictly: true,
})
const clear = () => {
  ruleForm= reactive({
    id:'',
    name: '',
    slug: ''
  })
}
const listData = ref([])
const getListFn = async () => {
  listData.value = await getList(formInline)
  console.log(listData.value)
}
const selectPermissionList = ref([])
const handleEdit = async(row) => {
  clear()
  const res = await detail({id:row.id})
  selectPermissionList.value = await selectPermission()
  Object.assign(ruleForm, {id:res.id, name: res.name, slug: res.slug})
  dialogVisible.value = true
}

const handleCreate = async () => {
  clear()
  selectPermissionList.value = await selectPermission()
  dialogVisible.value = true
}
const handleClose = () => {
  clear()
  dialogVisible.value = false
}
const handelSubmit = async () => {
  const api = ruleForm.id !== '' && ruleForm.id !== undefined ? updatePermission : createPermission
  const res = await api({...ruleForm})
  if (res.code === '000') {
    getListFn();
    handleClose()
    ElMessage({
      message: '新增成功',
      type: 'success',
    })
  }
}


function handleChange(value) {
  ruleForm.pid = value[value.length - 1]
}
getListFn();

</script>

<style scoped>

</style>